##########################
# Nadiya Kostyuk
# Summary Statistics
# Online Appendix
# R version: 4.1
# 11/12/25
##########################


rm(list=ls())
library(corrplot)
library(dplyr)


setwd("")


load('data/data_final.RData')
head(data_final)

data_final = data_final %>%
  # create dummies for each war phase:
  mutate(
    `Offensive Maneuver (R)`  = ifelse(war_phases == 'Rus Maneuver Offensive', 1, 0), 
    `Defensive Maneuver (R)`  = ifelse(war_phases == 'Rus Maneuver Defensive', 1, 0), 
    `Attrition`  = ifelse(war_phases == 'Attrition', 1, 0), 
  )


data_matrix = data_final %>%
  ungroup() %>%
  dplyr::select(
    disruption_intentional_3indicators, 
    `Offensive Maneuver (R)`, `Defensive Maneuver (R)`, `Attrition`,
    rus_mil_t, holiday_dummy, WEEKEND
  )
summary(data_matrix)

names(data_matrix) <- c(
  'Rus Internet Outage', 
  'Rus Offensive Maneuver', 'Rus Defensive Maneuver', 'Attrition', 
  "Rus Kinetic", 'Holidays', 'Weekend'
)

corr_matrix <- cor(data_matrix, use = "complete.obs")

corrplot(
  corr_matrix,
  method="number", # 'shade'
  number.cex=0.8,
  tl.cex=0.8
)



# summary stats:
main_varz = c(
   'disruption_intentional_3indicators', 
   'Offensive Maneuver (R)', 
   'Defensive Maneuver (R)', 'Attrition',
  'rus_mil_t','holiday_dummy', 'WEEKEND'
)

tab = summary(data_final[, main_varz])
dim(tab)
# creating latex table: 
tab = tab[c(1, 3, 4, 6),]
tab
class(tab)
dim(tab)
tab = as.data.frame(tab)
tab = tab %>%
  mutate(Freq=as.numeric(sapply(strsplit(Freq, ":"),"[[",2)))%>%
  mutate(Freq=round(Freq, digits = 2))
tab

# from long to wide:
tab_wide = tab %>%
  mutate(ID=seq(1, 28, by=1))%>%
  tidyr::spread(Var2, Freq)%>%
  dplyr::select(-c(--ID, Var1))
# vertically
#tab_wide = sapply(tab_wide, function(x) {x[!is.na(x)]})
#rownames(tab_wide) = c('Minimum', 'Median', 'Mean', 'Maximum')
# horizontally:
tab_wide = lapply(tab_wide, function(x) {x[!is.na(x)]}) 
tab_wide = do.call(rbind, tab_wide)
colnames(tab_wide) = c('Minimum', 'Median', 'Mean', 'Maximum')
tab_wide

library(xtable)
xtable(tab_wide)
#writeLines(capture.output(xtable(tab_wide)), paste0('Analysis/Results/summary_stats.tex'))

